package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.StatsStoreService;
import com.huilian.iotbox.admin.wap.service.StatsUserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.StatsUserDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.concurrent.ExecutionException;

/**
 * @author 谢洛涛
 * @date 2021/5/8 10:04
 */
@RestController
@RequestMapping("/api/statsUser")
public class StatsUserController extends UserInfoController {
    @Autowired
    private StatsUserService statsUserService;
    @Autowired
    private StatsStoreService statsStoreService;


    /**
     * 运营统计-小程序专用
     *
     * @param request
     * @param statsUserDto
     * @return
     */
    @PostMapping("/findStatsUserList")
    public ServerResponse findStatsUserList(HttpServletRequest request, @RequestBody StatsUserDto statsUserDto) throws ParseException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(statsUserService.findStatsUser(statsUserDto));
    }

    /**
     * 运营统计-小程序专用v2
     *
     * @param request
     * @param statsUserDto
     * @return
     */
    @PostMapping("/findStatsUserListTwo")
    public ServerResponse findStatsUserListTwo(HttpServletRequest request, @RequestBody StatsUserDto statsUserDto) throws ParseException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(statsUserService.findStatsUserTwo(statsUserDto));
    }


    @GetMapping("/findThisMonthTurnover")
    public ServerResponse findThisMonthTurnover(HttpServletRequest request) {
        StatsUserDto statsUserDto = new StatsUserDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            return ServerResponse.createBySuccess(statsStoreService.findThisMonthTurnover(statsUserDto));
        } else {
            return ServerResponse.createBySuccess(statsUserService.findThisMonthTurnover(statsUserDto));
        }
    }

    @GetMapping("/findByDateStore")
    public ServerResponse findByDateStore(HttpServletRequest request, String dateStr) {
        StatsUserDto statsUserDto = new StatsUserDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        statsUserDto.setDateStr(dateStr);
        String time = statsUserDto.getDateStr() + "-01";
        statsUserDto.setStartDateStr(DateTimeUtils.getSomeMonthFirstDay(time));
        statsUserDto.setEndDateStr(DateTimeUtils.getSomeMonthLastDay(time));
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            // 统计子账号管理的门店
            if (userInfo.getStoreIdList().size() > 0) {
                return ServerResponse.createBySuccess(statsStoreService.findByDateStore(statsUserDto));
            } else {
                return ServerResponse.createByErrorMessage("该管理账号下暂无店铺");
            }
        } else {
            return ServerResponse.createBySuccess(statsUserService.findByDateStore(statsUserDto));
        }

    }

    /**
     * 运营统计分支，点击 投币，出币，营业额等进入详情统计-头部
     *
     * @param request
     * @param statsUserDto
     * @return
     */
    @PostMapping("/findStatsBranchHead")
    public ServerResponse findStatsBranchHead(HttpServletRequest request, @RequestBody StatsUserDto statsUserDto) throws ParseException, ExecutionException, InterruptedException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        return statsUserService.findStatsBranchHead(statsUserDto);
    }

    /**
     * 运营统计分支，点击 投币，出币，营业额等进入详情统计-中部
     *
     * @param request
     * @param statsUserDto
     * @return
     */
    @PostMapping("/findStatsBranchData")
    public ServerResponse findStatsBranchData(HttpServletRequest request, @RequestBody StatsUserDto statsUserDto) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        return statsUserService.findStatsBranchData(statsUserDto);
    }
}
